﻿using PearTunes.WEB.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using PearTunes.Model;
using System.Collections.Generic;
using System.Linq;

namespace PearTunes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for UserModelTest and is intended
    ///to contain all UserModelTest Unit Tests
    ///</summary>
    [TestClass()]
    public class UserModelTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for AddMoney
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void AddMoneyTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            context.Users.Add(new User() { Name = "AddMoneyTest", Money = 0 });
            context.SaveChanges();
            (new UserModel()).AddMoney("AddMoneyTest", 15);
            context = new DAL.PearTunesEntities();
            int expected = 15;
            int actual = context.Users.Where(u => u.Name == "AddMoneyTest").First().Money;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for AddUser
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void AddUserTest()
        {
            UserModel target = new UserModel();
            string username = "AddUserTest";
            string email = "AddUserTest";
            int money = 0;
            string password = "AddUserTest";
            target.AddUser(username, email, money, password);
            int expected = 1;
            int actual = (new DAL.PearTunesEntities()).Users.Where(u => u.Name == "AddUserTest").Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ListUsers
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void ListUsersTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            int expected = 0;
            if (context.Users.Count() > 0)
                expected = context.Users.Count();
            else
            {
                context.Users.Add(new User() { Name = "ListUsersTest1" });
                context.Users.Add(new User() { Name = "ListUsersTest2" });
                context.Users.Add(new User() { Name = "ListUsersTest3" });
                context.SaveChanges();
                expected = 3; 
            }
            UserModel target = new UserModel();
            int actual = target.ListUsers().Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for UserInfo
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void UserInfoTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            User expected = new User() { Name = "UserInfoTest" };
            context.Users.Add(expected);
            context.SaveChanges();
            User actual = (new UserModel()).UserInfo("UserInfoTest");
            Assert.AreEqual(expected.Name, actual.Name);
        }

        /// <summary>
        ///A test for DeleteUser
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void DeleteUserTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            context.Users.Add(new User() { Name = "DeleteUserTest" });
            context.SaveChanges();
            UserModel target = new UserModel();
            int userId = (new DAL.PearTunesEntities()).Users.Where(u => u.Name == "DeleteUserTest").First().UserId;
            target.DeleteUser(userId);
            int expected = 0;
            int actual = (new DAL.PearTunesEntities()).Users.Where(u => u.Name == "DeleteUserTest").Count();
            Assert.AreEqual(expected, actual);
        }
    }
}
